﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class AltaDePaciente : Form
    {
        Form previous;
        public AltaDePaciente(Form p)
        {
            InitializeComponent();
            previous = p;
        }

        private void pacienteBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {

        }

        private void AltaDePaciente_Load(object sender, EventArgs e)
        {
            previous.Hide();
            this.Show();

            cmbEstado.SelectedIndex = 0;
            cmbEstadoCivil.SelectedIndex = 0;
            cmbSexo.SelectedIndex = 0;
            cmbOcupacion.SelectedIndex = 0;

        }

        private String validaCampos()
        {
            foreach (Control c in this.Controls)
                if (c is MaskedTextBox && !c.Name.Equals("mtbCorreo") && !c.Name.Equals("mtbTelefono") && String.IsNullOrEmpty(c.Text))
                    return "Alguno de los campos está vacío";            

            return null;
        }

        private void btnGuardar_Click(object sender, EventArgs e)
        {
            String vc = validaCampos();
            
            if (vc == null)
            {
                EmbeddedDatabase db = EmbeddedDatabase.Instance;                
                db.ExecuteNonQueryOperation("INSERT Paciente " +
                    "(Nombre, Direccion, Sexo, EstadoCivil, Fecha_nac, Ocupacion, Localidad, Estado, CP, Telefono, Correo, ApellidoPaterno, ApellidoMaterno)" +
                    " VALUES (" +
                    "'" + mtbNombre.Text + "'," +
                    "'" + mtbDireccion.Text + "'," +
                    "'" + cmbSexo.SelectedIndex + "'," +
                    "'" + cmbEstadoCivil.SelectedIndex + "'," +
                    "'" + fecha_nacDateTimePicker.Value + "'," +
                    "'" + cmbOcupacion.SelectedIndex + "'," +
                    "'" + mtbLocalidad.Text + "'," +
                    "'" + cmbEstado.SelectedIndex + "'," +
                    "'" + mtbCP.Text + "'," +
                    "'" + mtbTelefono.Text + "'," +
                    "'" + mtbCorreo.Text + "'," +
                    "'" + mtbApellidoPaterno.Text + "'," +                   
                    "'" + mtbApellidoMaterno.Text + "')");

                this.Hide();
                previous.Show();
            }
            else
                MessageBox.Show(vc, "Error al validar datos", MessageBoxButtons.OK);

        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            this.Hide();
            previous.Show();
        }
    }
}
